require 'song'
require 'link'
require 'mechanize'


class Songs < Hash
  PAGE_NAME = 'page/html'
  
  def to_yml
    puts 'Yamling ....'
    YAML::dump( self )
  end

  def to_unyml(filename)
    puts 'UnYamling ....'
    puts filename
     self.replace(YAML::load(open(filename).read))
  end
end

class Page
  attr_accessor :songs
  def initialize(url)
      @songs = Songs.new
      self.get(url).links.each do |mlink|
      link = Link.new mlink
      if link.valid?
        if @songs[link.get_caption].nil?
          @songs[link.get_caption] = Song.new(:rm => link.rm, :ram => link.ram,:caption => link.caption, :title => mlink.text) 
        else
          @songs[link.get_caption].set_rm_ram link
        end
      end
    end
  end
  
  def self.get(url)
    WWW::Mechanize.new.get(url)
  end
  
  def to_html
    puts 'Htmling ...'
    result = '<style type="text/css">.lsong {text-align:center;}</style>'
    result += '<style type="text/css">.tcolumn {text-align:center;font-weight:bold;}</style>'
    result += '<table width="100%"><tr><td><div class="tcolumn">MP3/POD Donwload</div></td><td><div class="tcolumn">listen</div></td>'
    @songs.each_value {|song|
      result += "<tr><td><div class='lsong'><a href='#{song.mp3}' style='text-decoration:none'>#{song.title}</a></div></td><td><div class='lsong'><a href='#{song.ram}' style='text-decoration:none'>#{song.title}</a></div></td>" if song.mp3
    }
    result += '</table>'
  end
end

#page = Page.new "http://www.6arab.com/singers-ar/east/nawal/index-ar.shtml"
open(".\\masica.htm","wb").write(Page.get('http://www.3arabiamusic.com/singers/index.html'))

